<?php

namespace Classes;

use Classes\autoload\SplClassLoader;
use Classes\db\DBConnection;
use Classes\UserPrincipal;
use PDO;
use Exception;

class ChatOperations{
	public static function submitMsg($user_id, $user, $msg){
		$connection = DbConnection::getDbConnection();

		
		$query = "INSERT INTO chatbox(`user_id`,
								`msg`,
								`msg_date`,
								`msg_time`)
				VALUES (:USER_ID,
						:MSG,
						curdate(),
						curtime())";
		
		$statement = $connection->prepare($query);
		$statement->bindValue(":USER_ID", $user_id, PDO::PARAM_INT);
		$statement->bindValue(":MSG", $msg, PDO::PARAM_STR);
		$statement->execute();
	}
	
	public static function displayLogs(){

		$connection = DbConnection::getDbConnection();
		$query2 = "	SELECT 	u.user_name as name,
								c.msg,
								c.msg_date,
								c.msg_time
						FROM chatbox c
						inner join user u using (user_id)
						WHERE 1
						ORDER BY msg_id desc";
		$statement = $connection->prepare($query2);
		$statement->execute();
		
		echo '<table>';
		while ($row = $statement->fetch() ){
			echo '<tr><td class="chat_user">'.$row['name'].': </td><td class = "chat_msg">'.$row['msg'].'</td><td class = "chat_time">'.$row['msg_time'].'</td>' ;
		}
		echo '</table>';
		
	}
}